package com.gy.smart.tool.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gy.smart.R;
import com.gy.smart.tool.bean.ToolBean;
import com.gy.smart.util.ListViewItemListener;
import com.gy.smart.view.MyFullGridView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 作者：gy on 17/1/4
 * 说明：工具父类adapter
 */
public class ToolAdapter extends BaseAdapter {

    private Context context;
    private List<ToolBean> datas;
    private ListViewItemListener listener;

    public ToolAdapter(Context context,List<ToolBean> datas){
        this.context = context;
        this.datas = datas;
    }

    public void setListener(ListViewItemListener listener){
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_tool_parent,null);
            holder.imgFlag = (ImageView) convertView.findViewById(R.id.adapter_tool_parent_img_flag);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.adapter_tool_parent_tv_title);
            holder.gridView = (MyFullGridView) convertView.findViewById(R.id.adapter_tool_parent_gridview);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        holder.imgFlag.setImageResource(datas.get(position).imgRes);
        ImageLoader.getInstance().displayImage(datas.get(position).tool_img_url,holder.imgFlag);
        holder.tvTitle.setText(datas.get(position).tool_name);
        ToolChildAdapter adapter = new ToolChildAdapter(context,datas.get(position).toolData);
        adapter.setListener(new ListViewItemListener() {
            @Override
            public void doPassActionListener(Object obj, int org1, int org2, String str) {
                listener.doPassActionListener(null,position,org2,str);
            }
        });
        holder.gridView.setAdapter(adapter);
        return convertView;
    }

    class ViewHolder{
        ImageView imgFlag;
        TextView tvTitle;
        MyFullGridView gridView;
    }
}
